/**
 * Licensed to Apereo under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright ownership. Apereo
 * licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the License at the
 * following location:
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apereo.portal.url;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apereo.portal.IUserPreferencesManager;
import org.apereo.portal.IUserProfile;
import org.apereo.portal.layout.dao.IStylesheetDescriptorDao;
import org.apereo.portal.layout.om.IStylesheetDescriptor;
import org.apereo.portal.user.IUserInstance;
import org.apereo.portal.user.IUserInstanceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** Simple autowired registry of {@link IUrlNodeSyntaxHelper} beans. */
@Service
public class UrlNodeSyntaxHelperRegistryImpl implements IUrlNodeSyntaxHelperRegistry {
    private IUserInstanceManager userInstanceManager;
    private IStylesheetDescriptorDao stylesheetDescriptorDao;
    private Map<String, IUrlNodeSyntaxHelper> urlNodeSyntaxHelpers = Collections.emptyMap();

    @Autowired
    public void setUrlNodeSyntaxHelpers(Collection<IUrlNodeSyntaxHelper> urlNodeSyntaxHelpers) {
        final Map<String, IUrlNodeSyntaxHelper> urlNodeSyntaxHelperBuilder =
                new LinkedHashMap<String, IUrlNodeSyntaxHelper>();

        for (final IUrlNodeSyntaxHelper urlNodeSyntaxHelper : urlNodeSyntaxHelpers) {
            urlNodeSyntaxHelperBuilder.put(urlNodeSyntaxHelper.getName(), urlNodeSyntaxHelper);
        }

        this.urlNodeSyntaxHelpers = Collections.unmodifiableMap(urlNodeSyntaxHelperBuilder);
    }

    @Autowired
    public void setUserInstanceManager(IUserInstanceManager userInstanceManager) {
        this.userInstanceManager = userInstanceManager;
    }

    @Autowired
    public void setStylesheetDescriptorDao(IStylesheetDescriptorDao stylesheetDescriptorDao) {
        this.stylesheetDescriptorDao = stylesheetDescriptorDao;
    }

    /* (non-Javadoc)
     * @see org.apereo.portal.url.IUrlNodeSyntaxHelperRegistry#getUrlNodeSyntaxHelper(java.lang.String)
     */
    @Override
    public IUrlNodeSyntaxHelper getUrlNodeSyntaxHelper(String name) {
        return this.urlNodeSyntaxHelpers.get(name);
    }

    @Override
    public IUrlNodeSyntaxHelper getCurrentUrlNodeSyntaxHelper(HttpServletRequest request) {
        final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
        final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
        final IUserProfile userProfile = preferencesManager.getUserProfile();

        final int themeStylesheetId = userProfile.getThemeStylesheetId();
        final IUrlNodeSyntaxHelper themeUrlSyntaxHelper =
                getUrlNodeSyntaxHelperForStylesheet(themeStylesheetId);
        if (themeUrlSyntaxHelper != null) {
            return themeUrlSyntaxHelper;
        }

        final int structureStylesheetId = userProfile.getStructureStylesheetId();
        final IUrlNodeSyntaxHelper structureUrlSyntaxHelper =
                getUrlNodeSyntaxHelperForStylesheet(structureStylesheetId);
        if (structureUrlSyntaxHelper != null) {
            return structureUrlSyntaxHelper;
        }

        throw new IllegalStateException(
                "No IUrlNodeSyntaxHelper could be found for the current request. Review the IStylesheetDescriptor configuration.");
    }

    @Override
    public IUrlNodeSyntaxHelper getUrlNodeSyntaxHelperForStylesheet(
            final int stylesheetDescriptorId) {
        final IStylesheetDescriptor stylesheetDescriptor =
                this.stylesheetDescriptorDao.getStylesheetDescriptor(stylesheetDescriptorId);
        if (stylesheetDescriptor == null) {
            throw new IllegalArgumentException(
                    "No IStylesheetDescriptor found for id: " + stylesheetDescriptorId);
        }

        final String themeUrlSyntaxHelperName = stylesheetDescriptor.getUrlNodeSyntaxHelperName();
        if (themeUrlSyntaxHelperName != null) {
            return this.getUrlNodeSyntaxHelper(themeUrlSyntaxHelperName);
        }

        return null;
    }
}
